
package com.sun.showcase.biz.basic.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.basic.dao.GroupResourceDao;
import com.sun.showcase.biz.basic.service.GroupResourceService;
import com.sun.showcase.client.domain.basic.GroupResource;
import com.sun.showcase.client.domain.basic.RoleResource;
import com.sun.showcase.client.query.basic.GroupResourceQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.LoginContextHolder;
import com.sun.showcase.utils.StringConstant;
@Service("groupResourceService")
@Transactional
public class GroupResourceServiceImpl implements GroupResourceService{
	@Autowired
	private GroupResourceDao groupResourceDao;
	
	public void setGroupResourceDao(GroupResourceDao dao) {
		this.groupResourceDao = dao;
	}

	public DataGrid datagrid(GroupResourceQuery groupResourceQuery) {
		DataGrid j = new DataGrid();
		Pager<GroupResource> pager  = find(groupResourceQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<GroupResourceQuery> getQuerysFromEntitys(List<GroupResource> groupResources) {
		List<GroupResourceQuery> groupResourceQuerys = new ArrayList<GroupResourceQuery>();
		if (groupResources != null && groupResources.size() > 0) {
			for (GroupResource tb : groupResources) {
				GroupResourceQuery b = new GroupResourceQuery();
				BeanUtils.copyProperties(tb, b);
				groupResourceQuerys.add(b);
			}
		}
		return groupResourceQuerys;
	}

	private Pager<GroupResource> find(GroupResourceQuery groupResourceQuery) {
		Page<GroupResource> page = PageHelper.startPage(groupResourceQuery.getPage().intValue(), groupResourceQuery.getRows().intValue());
		List<GroupResource> list = groupResourceDao.findList(groupResourceQuery);
		Pager<GroupResource> pager = new Pager<GroupResource>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public GroupResource add(GroupResourceQuery groupResourceQuery) {
		GroupResource t = new GroupResource();
		BeanUtils.copyProperties(groupResourceQuery, t);
		groupResourceDao.save(t);
		groupResourceQuery.setGroupId(t.getGroupId());
		return t;
		
	}

	public void update(GroupResourceQuery groupResourceQuery) {
		/*GroupResource t = groupResourceDao.getById(groupResourceQuery.getGroupId());
	    if(t != null) {
	    	BeanUtils.copyProperties(groupResourceQuery, t);
	    	groupResourceDao.update(t);
		}*/
		//删除组下的所有资源
		groupResourceDao.deleteByGroupId(groupResourceQuery.getGroupId());
		 //新增角色资源关系表数据
	    String resourceIds[]=groupResourceQuery.getRsIds().split(",");
		for(int i=0;i<resourceIds.length;i++){
			if(!(resourceIds[i]==null||"".equals(resourceIds[i]))){
				GroupResource groupResource = new GroupResource();
				groupResource.setGroupId(groupResourceQuery.getGroupId());
				groupResource.setResourceId(resourceIds[i]);
				groupResourceDao.save(groupResource);
			}
		}
		
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(GroupResourceQuery groupResourceQuery) {
	    groupResourceDao.updatePart(groupResourceQuery);
	}

	public void delete(java.lang.String[] ids) {
		groupResourceDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		groupResourceDao.deletePt(ids);
	}

	public GroupResource get(GroupResourceQuery groupResourceQuery) {
		return groupResourceDao.getById(groupResourceQuery.getGroupId());
	}

	public GroupResource get(String id) {
		return groupResourceDao.getById(id);
	}

	
	public List<GroupResourceQuery> listAll(GroupResourceQuery groupResourceQuery) {
		groupResourceQuery.setOrderColum("GROUP_ID");
	    List<GroupResource> list = groupResourceDao.findList(groupResourceQuery);
		List<GroupResourceQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	@Override
	public void deleteByResourceId(String resourceId) {
		// TODO Auto-generated method stub
		groupResourceDao.deleteByResourceId(resourceId);
		
	}

	@Override
	public String[] findByCheckedIds(String groupId) {
		
		return groupResourceDao.findByCheckedIds(groupId);
	}
	
}
